﻿using System.Collections.ObjectModel;
using System.Threading.Tasks; 
using System.Web.Mvc;
using OPM.Class;
using OPM.Models;

namespace OPM.Controllers
{ 
    public class AccountController : Controller
    {
        public ActionResult Login()
        {
            return View();
        } 

        [HttpPost]
        public ActionResult Login(string userName, string password)
        {
            return LoginPrivate(userName, password);
        }

        private ActionResult LoginPrivate(string userName, string password)
        {
            if (!string.IsNullOrWhiteSpace(userName) && UserManager.Login(userName, password))
            {
                UrlHelper u = new UrlHelper(this.ControllerContext.RequestContext);
                string url = u.Action("Index", "Home", null);

                return Content(Util.RenderTagMetaRedirect(url, "Home"));
            }
            ViewBag.userName = userName;
            ViewBag.password = password;
            ViewBag.message = "ข้อมูลผู้ใช้ไม่ถูกต้อง";
            return View("Login");
        }
        public ActionResult Logoff()
        {
            UrlHelper u = new UrlHelper(this.ControllerContext.RequestContext);
            string url = u.Action("Login", "Account", null);

            return Content(Util.RenderTagMetaRedirect(url, "Login"));
        }
    }
}